Repository navigation
Index CurrentCycle's join-table lookups - #528
Conversation
tmfrnz
left a comment
There was a problem hiding this comment.
Correct - both tables only had updated_by_id indexed, and stale_parents filters and plucks on exactly these four columns.
Two notes, neither blocking.
The description says every lookup "was a sequential scan." That states what the planner did; what the schema shows is that no index was available. Postgres will likely still choose sequential scans here - recommendation_categories has 2655 rows and measure_indicators has 0 on sg-production, both small enough that a sequential scan beats an index lookup plus heap fetches. So this reads as schema correctness that matters as the tables grow, rather than a speedup today, and I'd frame it that way.
stale_parents filters on one column and plucks the other in both directions, so composites ([category_id, recommendation_id] plus the reverse, same for measure_indicators) would allow index-only scans and subsume two of these four - same number of indexes, just paired. Not worth changing at this size, noting it for later.
2b70a9d to
262bb0b
Compare
recommendation_categories and measure_indicators back CurrentCycle's hot queries but, unlike recommendation_measures, were never indexed on either foreign key - every lookup by category_id, recommendation_id, measure_id or indicator_id was a sequential scan over the whole table.
262bb0b to
ff89109
Compare
…552) Brings the `is_current` optimisation stack from sg-dev (#524, #525, #529, #528, #526) to the NZ branch. The 12 commits are cherry-picked unchanged apart from conflict resolutions, plus one NZ-specific spec fix. ## What it does - `is_current` for recommendations, measures, indicators and progress reports is resolved once per request by `CurrentCycle` instead of per record (N+1 at every hop, run twice per request: filter + serialiser). `Category#is_current` itself is unchanged in approach and runs once per reporting-cycle category. - Ordering of sibling cycles is well defined: undated cycles sort last, ties break on id (#525). - Archived cycle categories are excluded from the sibling comparison and are never current themselves (#529). - Indexes on `recommendation_categories` and `measure_indicators` foreign keys (#528). - `ResetsCurrentCycle` invalidates the per-request resolver on relevant writes only. ## NZ-specific changes - Conflict resolutions: NZ's category factory has no `:published` trait (draft defaults to false here); NZ's `only_manager_and_admin_users_can_be_assigned` validation kept. - Request specs sign in with the factory password and only stub `enable_mfa` where it exists. ## Tested on nz-uat Response sizes unchanged to the byte. Sibling logic additionally verified with test data (newest wins, drafts current, undated cycle no longer unseats dated ones, archived runner-up promoted, recs without a cycle stay current, propagation to measures/indicators), and cache invalidation confirmed in the running app. ## User-visible changes None expected with current NZ data (one cycle per treaty). In general: - An undated cycle no longer makes its dated siblings non-current. - Archived cycles, and recommendations linked only to them, drop out of `current_only`. - Progress reports without an indicator are excluded from `current_only` instead of erroring. --------- Co-authored-by: Philip Arndt <git@p.arndt.io>
) (#554) Releases the `is_current` optimisation stack (#524, #525, #529, #528, #526) from sg-dev to sg-production. Replaces #553. ## Why a release branch sg-production received the security changes in #533 as a single squash commit, so git did not recognise sg-dev's individual commits as already released and #553 showed 32 commits with conflicts. This branch is sg-dev plus a `-s ours` merge of sg-production, which records that sg-production is contained without changing any content. The diff against sg-production is the stack only. ## What changes - `is_current` is resolved once per request by `CurrentCycle` instead of per record and per serialisation (#526). - Undated reporting cycles no longer make their dated siblings non-current (#525). - Archived cycle categories drop out of the sibling comparison and are never current themselves (#529). - Indexes on `recommendation_categories` and `measure_indicators` foreign keys (#528). - Tests characterising `Category#is_current` and the propagation chain (#524). ## Tested on sg-uat Same change verified on nz-uat and nz-production (recommendations, measures, indicators): identical results, 15–40× faster in production. ## User-visible changes None expected with current data. In general: an undated cycle no longer hides its dated siblings' recommendations, and archived cycles (and recommendations linked only to them) drop out of `current_only`. --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Philip Arndt <git@p.arndt.io> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
recommendation_categoriesandmeasure_indicatorsbackCurrentCycle's hot queries but, unlikerecommendation_measures, were never indexed on either foreign key - every lookup bycategory_id,recommendation_id,measure_idorindicator_idwas a sequential scan over the whole table.